
/*
 * ThemesGui.java
 *
 * Created on Feb 27, 2011, 2:31:30 AM
 */
package org.rsbot.gui;

import java.io.File;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.rsbot.util.FrameUtil;
import org.rsbot.util.Serializer;
import org.rsbot.util.GlobalConfiguration;
import org.rsbot.util.UpdateUtil;

/**
 *
 * @author Nader Sleiman
 */
public class ThemesGui extends javax.swing.JFrame {

    private JFrame gui;
    private JFrame curr;
    private static final Logger log = Logger.getLogger(ThemesGui.class.getName());
    private static boolean updated;

    /** Creates new form ThemesGui */
    public ThemesGui(JFrame f) {
        gui = f;
        initComponents();
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(getOwner());
        setTitle("Themes Selector");
        addOptions();
        setResizable(false);
        curr = this;
    }

    protected static boolean isProcessing() {
        return th != null && th.isAlive();
    }

    protected static boolean isUpdated() {
        return updated;
    }

    private void addOptions() {
        jComboBox1.addItem("SubstanceAutumnLookAndFeel");
        jComboBox1.addItem("SubstanceNebulaLookAndFeel");
        jComboBox1.addItem("SubstanceNebulaBrickWallLookAndFeel");
        jComboBox1.addItem("SubstanceSaharaLookAndFeel");
        jComboBox1.addItem("SubstanceRavenLookAndFeel");
        jComboBox1.addItem("SubstanceOfficeSilver2007LookAndFeel");
        jComboBox1.addItem("SubstanceOfficeBlue2007LookAndFeel");
        jComboBox1.addItem("SubstanceModerateLookAndFeel");
        jComboBox1.addItem("SubstanceMistSilverLookAndFeel");
        jComboBox1.addItem("SubstanceMistAquaLookAndFeel");
        jComboBox1.addItem("SubstanceMagellanLookAndFeel");
        jComboBox1.addItem("SubstanceGraphiteLookAndFeel");
        jComboBox1.addItem("SubstanceGraphiteGlassLookAndFeel");
        jComboBox1.addItem("SubstanceGraphiteAquaLookAndFeel");
        jComboBox1.addItem("SubstanceGeminiLookAndFeel");
        jComboBox1.addItem("SubstanceEmeraldDuskLookAndFeel");
        jComboBox1.addItem("SubstanceDustLookAndFeel");
        jComboBox1.addItem("SubstanceDustCoffeeLookAndFeel");
        jComboBox1.addItem("SubstanceCremeLookAndFeel");
        jComboBox1.addItem("SubstanceCremeCoffeeLookAndFeel");
        jComboBox1.addItem("SubstanceChallengerDeepLookAndFeel");
        jComboBox1.addItem("SubstanceBusinessLookAndFeel");
        jComboBox1.addItem("SubstanceBusinessBlueSteelLookAndFeel");
        jComboBox1.addItem("SubstanceBusinessBlackSteelLookAndFeel");

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Select a theme, and press on save to have it on further bot boots:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBox1, 0, 380, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(124, 124, 124)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {

            Serializer.serlialize(jComboBox1.getSelectedItem().toString(), new File(GlobalConfiguration.Paths.Resources.THEME));
        } catch (Exception e) {
            e.printStackTrace();
        }
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    private String name = "";
    static private Thread th = null;
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        name = jComboBox1.getSelectedItem().toString();

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                if (!FrameUtil.setTheme(gui, name)) {
                    JOptionPane.showMessageDialog(null, " Couldn't load the look and feel Substance,please make ." + '\n' + "sure that dependancies are in the lib folder int the jar's folder.");
                    dispose();
                    int resp = JOptionPane.showConfirmDialog(null, "Do you want to download the dependency files to the lib" + '\n' + "folder in this jar's directory?" + '\n' + "you have to restart bot right after", "Question",
                            JOptionPane.YES_NO_OPTION);
                    if (resp == JOptionPane.YES_OPTION) {
                        File f = new File("lib");
                        try {
                            if (f.mkdir()) {
                                System.out.println("Directory Created");
                            }
                        } catch (Exception e) {
                            log.info("couldnt create lib file, try again please");
                            return;
                        }
                        th = new Thread(new Runnable() {

                            public void run() {

                                log.info("Downloading Themes Components, please wait ...");
                                if (UpdateUtil.download("http://f.nikkii.us/~pervyshuya/substance.jar", "lib/Substance.jar")) {
                                    if (UpdateUtil.download("http://f.nikkii.us/~pervyshuya/trident.jar", "lib/trident.jar")) {
                                        log.info("Downloaded needed dependancies! ,please restart bot and try selecting themes again!");
                                        updated = true;
                                    }
                                }
                            }
                        });
                        th.start();
                    }

                }
                SwingUtilities.updateComponentTreeUI(curr);
            }
        });
    }//GEN-LAST:event_jComboBox1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
